package com.ma.chapter4.t5;

import lombok.extern.slf4j.Slf4j;

import static com.ma.util.Sleeper.sleep;

/**
 * @ClassName TestWaitNotifyall
 * @Author: mayongqiang
 * @DATE 2022/3/12 19:59
 * @Description:
 */
@Slf4j(topic = "m.test")
public class TestWaitNotifyall {
    static final Object obj = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (obj) {
                log.debug("执行....");
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其它代码...."); // 断点
            }
        }, "t1").start();
        new Thread(() -> {
            synchronized (obj) {
                log.debug("执行....");
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其它代码...."); // 断点
            }
        },"t2").start();

        sleep(0.5);
        log.debug("唤醒 obj 上其它线程");
        synchronized (obj) {
            obj.notifyAll(); // 唤醒obj上所有等待线程 断点
        }
    }
}
